package com.zerosky.crm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.DailyDAO;
import com.zerosky.crm.po.Daily;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.service.DailyService;
import com.zerosky.crm.util.PageInfo;

/**
 * Daily业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="dailyService")
public class DailyServiceImpl implements DailyService {

	@Autowired
	private DailyDAO dailyDAO; // Daily数据接口

	/**
	 * Daily列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Daily> getDailyList(Map map, PageInfo<Daily> page) {
		int begin = ((page.getPageNo() - 1) * page.getPageSize());
		int pageSize = page.getPageSize();

		// 写入数据
		map.put("pageNo", begin);
		map.put("pageSize", pageSize);
		List<Daily> list = dailyDAO.getDailyList(map);
		page.setList(list);

		int count = dailyDAO.countDaily(map);// 获取总记录
		page.setPageCount(count);
		return page;
	}

	/**
	 * 通过主键查找Daily实体
	 * 
	 * @param id
	 *            Daily主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Daily getDailyById(String id) {
		return dailyDAO.getDailyById(id);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveDaily(Employee user, String content, String title, int status) throws Exception {
		// 初始化日期、周期、月份
		String fillDate = "--";
		String cycle = "--";
		String month = "--";
		Calendar calendar = Calendar.getInstance();
		// status为1:日报，2：周报，3：月报
		if (status == 1) {
			// 设置日期格式，获取当前日期
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			fillDate = df.format(new Date());
		} else if (status == 2) {
			// 获取本周为当前年的第几周
			calendar.setTime(new Date());
			cycle = String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
		} else {
			// 获取当前月份
			month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		}
		// 创建对象，设置参数
		Daily daily = new Daily();
		daily.setFillDate(fillDate);
		daily.setContent(content);
		daily.setTitle(title);
		daily.setCycle(cycle);
		daily.setMonth(month);
		daily.setDailyState("0");
		daily.setFillId(user.getEmployeeId());
		daily.setFillName(user.getName());
		daily.setDeptId(user.getDept() != null ? user.getDept().getDeptId() : null);
		daily.setDeptName(user.getDept() != null ? user.getDept().getDeptName() : null);
		daily.setBranchId(user.getBranch() != null ? user.getBranch().getBranchId() : null);
		daily.setBranchName(user.getBranch() != null ? user.getBranch().getBranchName() : null);
		daily.setTeamId(user.getTeam() != null ? user.getTeam().getBranchId() : null);
		daily.setTeamName(user.getTeam() != null ? user.getTeam().getBranchName() : null);
		daily.setStatus(status);
		daily.setCreateTime(new Date());
		daily.setCreateUserId(user.getEmployeeId());

		dailyDAO.addDaily(daily);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void updateDaily(Daily daily) throws Exception {
		dailyDAO.updateDaily(daily);
	}

	/**
	 * 批量删除Daily
	 * 
	 * @param idArray
	 *            Daily主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteDaily(String date) throws Exception {
		dailyDAO.deleteDaily(date);
	}
}
